package com.tubang.logistics.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.tubang.common.constant.Constants;
import com.tubang.common.util.BeanMapper;
import com.tubang.common.util.CollectionUtils;
import com.tubang.common.util.IdGenerator;
import com.tubang.common.util.StringUtils;
import com.tubang.logistics.entity.GoodsEntity;
import com.tubang.logistics.entity.PlatformOrderEntity;
import com.tubang.logistics.entity.PlatformOrderGoodsEntity;
import com.tubang.logistics.entity.ShipperOrderEntity;
import com.tubang.logistics.mapper.PlatformOrderMapper;
import com.tubang.logistics.service.IGoodsService;
import com.tubang.logistics.service.IPlatformOrderGoodsService;
import com.tubang.logistics.service.IPlatformOrderService;
import com.tubang.logistics.service.IShipperOrderService;
import com.tubang.logistics.vo.ChangeStatusVo;
import com.tubang.logistics.vo.OrderDispatchVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 平台运单表 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2017-12-31
 */
@Service
public class PlatformOrderServiceImpl extends ServiceImpl<PlatformOrderMapper, PlatformOrderEntity> implements IPlatformOrderService {

    @Resource
    private IShipperOrderService shipperOrderService;

    @Resource
    private IGoodsService goodsService;

    @Resource
    private IPlatformOrderService platformOrderService;

    @Resource
    private IPlatformOrderGoodsService platformOrderGoodsService;


    @Override
    public void updateplatformOrderStatus(ChangeStatusVo changeStatusVo) {
        PlatformOrderEntity order = selectById(changeStatusVo.getOrderId());
        if (order == null) return;
        order.setStatusCode(changeStatusVo.getThisStatus());
        order.setStatusCodeDesc(changeStatusVo.getThisStatusDesc());
        Double carriage = changeStatusVo.getCarriage();
        if (carriage != null) {
            order.setCarriage(carriage);
        }
        String remarks = changeStatusVo.getRemarks();
        if (StringUtils.isNotEmpty(remarks)) {
            order.setRemarks(remarks);
        }
        String cid = changeStatusVo.getCid();
        if (StringUtils.isNotEmpty(cid)) {
            order.setCarrier(changeStatusVo.getCname());
            order.setCarrierId(cid);
            order.setCarrierMobile(changeStatusVo.getCmobile());
        }

        insertOrUpdate(order);
    }

    @Override
    public void creatPlatformOrder(OrderDispatchVo orderDispatchVo) {
        Date now = new Date();
        List<String> goodsIds = orderDispatchVo.getGoodsIds();
        PlatformOrderEntity platformOrderEntity = new PlatformOrderEntity();
        platformOrderEntity.setStatusCode(String.valueOf(orderDispatchVo.getStatus()));
        platformOrderEntity.setCreateTime(now);
        platformOrderEntity.setEnable(true);
        platformOrderEntity.setId(null);
        insertOrUpdate(platformOrderEntity);
        String platformId = platformOrderEntity.getId();
        //货品重量
        BigDecimal goodsWeight = new BigDecimal(0);
        //货品接单单价
        Double carriage = 0d;
        for (String goodsId : goodsIds) {
            //查询原单数据
            GoodsEntity goodsEntity = goodsService.selectById(goodsId);
            if (goodsEntity == null) continue;
            //计算商品重量
            if (!goodsEntity.getParentId().equals("0")) {
                goodsWeight = goodsWeight.add(goodsEntity.getWeight());
            }
            //跟新货品状态
            goodsEntity.setGoodsStatus(Constants.DEFAULT_VAULE_ZERO);
            goodsService.insertOrUpdate(goodsEntity);
            ShipperOrderEntity shipperOrderEntity = shipperOrderService.selectById(goodsEntity.getShipperOrderId());
            //判断此商品所在订单是否有未调度的商品
            List<GoodsEntity> goodsEntities = goodsService.selectGoodssByOrderId(shipperOrderEntity.getId(), Constants.DEFAULT_VAULE_FU_ONE);
            if (CollectionUtils.isEmpty(goodsEntities)) {
                shipperOrderEntity.setStatusCode(String.valueOf(Constants.ORDER_STATUS_CODE_TAKE_GOODS));
                shipperOrderEntity.setStatusCodeDesc("待提货");
                shipperOrderService.insertOrUpdate(shipperOrderEntity);
            }
            carriage = shipperOrderEntity.getCarriageDeal();
            BeanMapper.copy(shipperOrderEntity, platformOrderEntity);
            //保存到新订单当中
            //保存关系表
            PlatformOrderGoodsEntity pfog = new PlatformOrderGoodsEntity();
            pfog.setPlatformOrderId(platformId);
            pfog.setGoodsId(goodsId);
            pfog.setCreateTime(now);
            pfog.setUpdateTime(now);
            pfog.setEnable(true);
            platformOrderGoodsService.insertOrUpdate(pfog);
        }

        String goodsName = orderDispatchVo.getGoodsName();
        if (goodsName != null) {
            platformOrderEntity.setGoodsName(goodsName);
        }
        //单价
        Double carriageChange = orderDispatchVo.getCarriage();
        if (carriageChange != null) {
            carriage = carriageChange;
        }
        platformOrderEntity.setGoodsWeight(goodsWeight.doubleValue());
        platformOrderEntity.setCarriage(carriage);
        platformOrderEntity.setAmount(Double.valueOf(new DecimalFormat(".##").format(carriage * goodsWeight.doubleValue())));
        platformOrderEntity.setCarriageDeal(carriage);
        platformOrderEntity.setAmountDeal(Double.valueOf(new DecimalFormat(".##").format(carriage * goodsWeight.doubleValue())));
        platformOrderEntity.setGoodsWeight(goodsWeight.doubleValue());
        platformOrderEntity.setId(platformId);
        platformOrderEntity.setStatusCode(String.valueOf(orderDispatchVo.getStatus()));
        platformOrderEntity.setStatusCodeDesc(orderDispatchVo.getStatusCodeDesc());
        platformOrderEntity.setCarrierMobile(orderDispatchVo.getCarrierMobile());
        //platformOrderEntity.setCarrierName(orderDispatchVo.getCarrierName());
        platformOrderEntity.setCarrierId(orderDispatchVo.getCarrierId());
        platformOrderEntity.setContainerType(orderDispatchVo.getCarrierType());
        platformOrderEntity.setUpdateTime(now);
        platformOrderEntity.setEnable(true);
        platformOrderEntity.setNum(IdGenerator.INSTANCE.nextId());
        platformOrderEntity.setUserId(orderDispatchVo.getUserId());
        platformOrderEntity.setUserName(orderDispatchVo.getUserName());
        String mobile = orderDispatchVo.getMobile();
        if(StringUtils.isEmpty(mobile)){
            mobile=orderDispatchVo.getUserMobile();
        }
        platformOrderEntity.setUserMobile(mobile);
        insertOrUpdate(platformOrderEntity);
    }
}
